package qianlong.practice.util;

import lombok.Data;

import java.util.logging.Logger;

@Data
public class PageUtil {
	
	private static final Logger logger = Logger.getLogger(PageUtil.class.getName());

	private int firstPage; //首页
	private int lastPage; //尾页
	private int previousPage; //上一页
	private int nextPage; //下一页	
	private int pageSize = 10; //每页条数，默认是10
	private int pageCount; //总页数
	private int currentPage; //当前页    = 请求页
	private int requestPage; //请求页
	
	//====================后台相关属性+数据库===================
	private int totalCount; //总记录数
	private int limit;  //分段取数据    开始索引  ===》分段取的起点
	private int offset; //分段取数据    分段条数  ===》分段取的终点 
	
	public void init(int requestPage, int pageSize, int totalCount) {
		
		
		this.requestPage = requestPage != 0 ? requestPage : 1; //赋值给请求页属性
		this.pageSize = pageSize != 0 ? pageSize : 10; //赋值给每页显示条数属性
		this.totalCount = totalCount; //总记录数
		
		logger.info("===========> this.requestPage =  " + this.requestPage);
		logger.info("===========> this.pageSize =  " + this.pageSize);
		logger.info("===========> this.totalCount =  " + this.totalCount);
		
		this.firstPage = 1; //首页  = 第一页	
		
		//------------------------------requestPage------------------------------------------
		this.currentPage = this.requestPage; //赋值给当前页属性    当前页 = 请求页		
		
		//当前页  = 第一页
		if(this.currentPage == this.firstPage) {
			this.previousPage = this.firstPage;
		}else {
			this.previousPage = this.currentPage - 1; //上一页  = 当前页  - 1
		}
		logger.info("===========> this.currentPage =  " + this.currentPage);
		//------------------------------pageSize + totalCount------------------
		this.offset = pageSize; //分段取数据  = pageSize		
		this.limit = (this.currentPage - 1) * this.pageSize; //分段取数据    开始索引 = (当前页 - 1)*每页显示条数
		logger.info("===========> 分段取数据  limit " + this.limit + ", " + this.offset);
		
		//总页数 = 总记录数  % 每页显示条数  != 0 ?  总记录数  / 每页显示条数 + 1 : 总记录数  / 每页显示条数;
		this.pageCount = this.totalCount % this.pageSize != 0 ? this.totalCount / this.pageSize + 1 : this.totalCount / this.pageSize;
		
		this.lastPage = pageCount; //尾页 = 总页数
		
		
		//当前页 = 尾页
		if(this.currentPage == this.lastPage) {
			this.nextPage = this.lastPage;
		}else {
			this.nextPage = this.currentPage + 1; //下一页  = 当前页  + 1
		}
		
		
		logger.info("===========> 总页数  pageCount = " + this.pageCount);
		
	}
//-------------------2.页面逻辑处理[总页数，初始化分页数据...]代码结束--------------------------	

}
